package control;

import Dao.RecordDao;
import Dao.RecordDaoimpl;
import com.alibaba.fastjson.JSON;
import entity.Record;
import entity.User;
import entity.Word;
import service.RecordService;
import service.RecordServiceimpl;
import service.SetService;
import service.SetServiceimpl;
import service.WordService;
import service.WordServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GetRecord extends HttpServlet {
    RecordService rsc =new RecordServiceimpl();
	WordService wsc = new WordServiceimpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String email = null;
		Integer typeid = null;;
		String password = null;
		for(Cookie c:req.getCookies()) {
			if(c.getName().equals("email")) {
				email = c.getValue();
			}
			if(c.getName().equals("typeid")) {
				typeid = Integer.valueOf(c.getValue());
			}
			if(c.getName().equals("password")) {
				password = c.getValue();
			}
		}
		String type = req.getParameter("type");
		Integer page = Integer.valueOf(req.getParameter("page"));
		List<Record> res = null;
		switch(type) {
			case "pass":{
				System.out.println("pass");
				try {
					res = rsc.getpass(email,page);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
			case "tod":{
				System.out.println("tod");
				try {
					res = rsc.todaywords(email);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
			case "rem":{
				System.out.println("rem");
				try {
					res = rsc.getseen(email,page);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
			
	    }
		
		System.out.println(res);
		List<Word> words = null;
		try {
			words = wsc.recordtowords(res);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(words);
		resp.setCharacterEncoding("utf-8");
		resp.getWriter().print(JSON.toJSON(words));
	}
	
}